home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 408_01 / rmgroup.c < prev    next >
Text File  |  1993-08-06  |  4KB  |  138 lines

  1. /*
  2.     SNEWS 1.91
  3.  
  4.     rmgroup - remove newsgroups from the active file, and delete the files
  5.  
  6.  
  7.     Copyright (C) 1991  John McCombs, Christchurch, NEW ZEALAND
  8.                         john@ahuriri.gen.nz
  9.                         PO Box 2708, Christchurch, NEW ZEALAND
  10.  
  11.     Modifications copyright (C) 1993  Daniel Fandrich
  12.                         <dan@fch.wimsey.bc.ca> or CompuServe 72365,306
  13.  
  14.     This program is free software; you can redistribute it and/or modify
  15.     it under the terms of the GNU General Public License, version 1, as
  16.     published by the Free Software Foundation.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     See the file COPYING, which contains a copy of the GNU General
  24.     Public License.
  25.  
  26.  
  27.     Source is formatted with a tab size of 4.
  28.  
  29.  */
  30.  
  31. #include "defs.h"
  32.  
  33. INFO my_stuff;
  34.  
  35.  
  36. /*---------------------------- main ---------------------------------------*/
  37. void main(int argc, char *argv[])
  38. {
  39.     /*
  40.      *  Delete a newsgroup.
  41.      *
  42.      *      - delete the data and index files
  43.      *      - re-write the active file, without that newsgroup, saving
  44.      *        the old one as .bak
  45.      *
  46.      *  This program could be structured to run faster, but I have tried
  47.      *  to do things so that if rmgroup croaks part way through, as little
  48.      *  damage will be done as possible.
  49.      *
  50.      */
  51.  
  52.     int    i;
  53.     char   buf[256], buf2[256], *dir, *p;
  54.     ACTIVE *gp;
  55.     FILE   *active_file, *new_active_file;
  56.  
  57.     signal(SIGINT, sig_break);        /* turn control-break off */
  58.  
  59.     fprintf(stderr, "RMGROUP: (%s)\n\n", VERSION);
  60.  
  61.     if ((argc > 1) &&
  62.         (argv[1][0] != '-') && (argv[1][0] != '/') && (argv[1][0] != '?')) {
  63.  
  64.         if (!load_stuff()) {
  65.             fprintf(stderr, "Couldn't read rc info\n");
  66.         }
  67.  
  68.         load_active_file();
  69.         close_active();
  70.  
  71.  
  72.         for (i = 1; i < argc; i++) {
  73.  
  74.             /* check that the group exists - also prevent del of 'junk' */
  75.             gp = find_news_group(argv[i]);
  76.  
  77.             if (stricmp(gp->group, "junk") != 0) {
  78.  
  79.                 /* form the directory name */
  80.                 dir = make_news_group_name(argv[i]);
  81.                 unlink(dir);
  82.                 sprintf(buf, "%s.IDX", dir);
  83.                 unlink(buf);
  84.  
  85.                 printf("rmgroup: %s: files gone... ", argv[i]);
  86.  
  87.                 /* finally remove it from the active file */
  88.                 sprintf(buf, "%sactive", my_stuff.news_dir);
  89.                 if ((active_file = fopen(buf, "rb")) == NULL) {
  90.                     fprintf(stderr, "\nrmgroup: cannot open %s\n", buf);
  91.                     exit(1);
  92.                 }
  93.  
  94.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  95.                 if ((new_active_file = fopen(buf, "wb")) == NULL) {
  96.                     fprintf(stderr, "\nrmgroup: cannot create %s\n", buf);
  97.                     exit(1);
  98.                 }
  99.  
  100.                 while (fgets(buf, 255, active_file) != NULL) {
  101.                     strcpy(buf2, buf);
  102.                     p = strtok(buf2, " \t");
  103.                     if (stricmp(argv[i], p) != 0) {
  104.                         if (fputs(buf, new_active_file) == EOF) {
  105.                             fprintf(stderr, "\nrmgroup: couldn't write new active file\n");
  106.                         }
  107.                     } else {
  108.                         printf("removed from active file\n",
  109.                                argv[i]);
  110.                     }
  111.                 }
  112.  
  113.                 fclose(active_file);
  114.                 fclose(new_active_file);
  115.  
  116.                 /* rename the active to a .bak and the .new to the active */
  117.                 sprintf(buf, "%sactive.bak", my_stuff.news_dir);
  118.                 unlink(buf);
  119.                 sprintf(buf2, "%sactive", my_stuff.news_dir);
  120.                 rename(buf2, buf);
  121.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  122.                 rename(buf, buf2);
  123.  
  124.  
  125.             } else {
  126.                 fprintf(stderr, "rmgroup: newsgroup %s not found\n", argv[i]);
  127.             }
  128.  
  129.         }
  130.  
  131.         close_active_file();
  132.  
  133.     } else {
  134.         printf("usage: rmgroup group.name ...\n");
  135.     }
  136.  
  137. }
  138.